<?php
/**
 * Created by PhpStorm.
 * User: luo
 * Date: 2018/9/30
 * Time: 16:19
 */

namespace app\models\traits;

use app\tools\Tool;
use Qiniu\Auth;
use Yii;
use yii\helpers\StringHelper;

trait Img
{
    public function showIcon($type='', $width = 400){
        $qiniu = Yii::$app->params['qiniu'];
        if($this->icon){
            return $qiniu['url'].$this->icon.'?imageMogr2/quality/95/thumbnail/'.$width.'x'.$width.'';
        }else if (!empty($type)) {
             return 'http://img.cyjxy.com/FsCy6FA6Lcw9KS3iFdjvuKLnoJEo.png';
        } else {
            return Yii::$app->params['avatar'];
        }
    }

    public function showSmallImg($attribute = 'small_img'){
        return $this->showImg($attribute);
    }

    public function showImg($attribute = 'img'){
        if($this->$attribute){
            return $this->show($this->$attribute);
        }else{
            return '';
        }
    }

    public function show($path){
        $qiniu = Yii::$app->params['qiniu'];
        if(!StringHelper::startsWith($path, 'http', false)){
            return $qiniu['url'].$path;
        }else{
            return $path;
        }
    }

    public function showWithWaterMark($path, $nickname, $use_water){
        $qiniu = Yii::$app->params['qiniu'];
        if(!StringHelper::startsWith($path, 'http', false)){
            return $this->setWaterMark($qiniu['url'].$path, $nickname, $use_water);
        }else{
            return $path;
        }
    }

    private function setWaterMark($url, $nickname, $use_water){
        if(Yii::$app->params['use_water_mark'] && strpos($url, 'watermark') === false && $use_water){
            $water_mark = Yii::$app->params['water_mark_img'];
            $water_image  = \Qiniu\base64_urlSafeEncode($water_mark);
            $url  = $url. "?imageView2/2".urlencode('|')."watermark/3/image/{$water_image}";
            $url .= "/gravity/SouthWest/dx/10/dy/10";
            $nickname = \Qiniu\base64_urlSafeEncode($nickname);
            $font     = \Qiniu\base64_urlSafeEncode('宋体');
            $fill    = \Qiniu\base64_urlSafeEncode('gray');
            $url     .= "/text/{$nickname}/font/{$font}/fontsize/360/fill/{$fill}/gravity/SouthWest/dx/47/dy/12";
            $fill2     = \Qiniu\base64_urlSafeEncode('#ffffff');
            $url     .= "/text/{$nickname}/font/{$font}/fontsize/360/fill/{$fill2}/gravity/SouthWest/dx/45/dy/14";

        }
        return $url;
    }


    public function showBigImg($attribute = 'big_img'){
        return $this->showImg($attribute);
    }

    public function onRemoveUrlPrefix()
    {
        $qiniu = Yii::$app->params['qiniu'];
        if($this->hasAttribute('icon')){
            $icon = str_replace($qiniu['url'], '', $this->icon);
            $this->icon = explode('?', $icon)[0];
        }
        if($this->hasAttribute('small_img')){
            $small_img  = str_replace($qiniu['url'], '', $this->small_img);
            $this->small_img = explode('?', $small_img)[0];

        }
        if($this->hasAttribute('img')){
            $img  = str_replace($qiniu['url'], '', $this->img);
            $this->img = explode('?', $img)[0];
        }
        if($this->hasAttribute('big_img')){
            $big_img  = str_replace($qiniu['url'], '', $this->big_img);
            $this->big_img = explode('?', $big_img)[0];
        }
    }

    public function showPrivateImg($field){
        $qiniu = Yii::$app->params['qiniu'];

        // 构建Auth对象
        $auth = new Auth($qiniu['access_key'], $qiniu['secret_key']);
        // 私有空间中的外链 http://<domain>/<file_key>
        $baseUrl = $this->$field;
        if($baseUrl){
            // 对链接进行签名
            $signedUrl = $auth->privateDownloadUrl($baseUrl);
            return $signedUrl;
        }else{
            return '';
        }
    }

}
